﻿<%@ WebHandler Language="C#" Class="login" %>

using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.SessionState;//第一步：导入此命名空间


public class login : IHttpHandler, IRequiresSessionState {
    // 声明成员属性
    Random r = new Random();
    public void ProcessRequest (HttpContext context) {
        //指定接收数据的类型
        context.Response.ContentType = "application/json";
        string type = context.Request["type"];
        //准备回送的字符串
        string outstr = "";

        if (type == "login")
        {

            //接收数据
            string username = context.Request["username"];
            string password = context.Request["password"];
            string vericode = context.Request["vericode"];

            //执行写入操作
            outstr = doSelect(username, password, vericode);
        }
        else
        {
            // 生成验证码
            //string verifiCode = r.Next(1000, 10000).ToString();
            //HttpContext.Current.Session["verifiCode"] = verifiCode;
            // outstr = verifiCode;
        }

        //回送结果
        context.Response.Write(outstr);
    }

    public string doSelect(string username, string password, string vericode)
    {
        
        string msg = "";
        string userCode = vericode.Trim().ToUpper().ToString();
        string verifiCode = HttpContext.Current.Session["verifiCode"].ToString().ToUpper();
        int i = 0;

        if (userCode != verifiCode)
        {
            msg = "验证码不正确";
        }
        else
        {
            string demo = MD5.getMD5(password);
            //执行插入操作，并获得影响的行数
            DataSet result = DBHelper.sqlMethod("select id,username,password,limit from admins where username = '" + username + "' and password = '" + MD5.getMD5(password) + "'");
            i = result.Tables[0].Rows.Count;
            if (i > 0)
            {
                msg = "登录成功";
                HttpContext.Current.Session["user_id"] = result.Tables[0].Rows[0][0].ToString();
                HttpContext.Current.Session["username"] = result.Tables[0].Rows[0][1].ToString();
                HttpContext.Current.Session["limit"] = result.Tables[0].Rows[0][3].ToString();
            }
            else
            {
                msg = "账号或密码错误";
            }
        }

        StringBuilder Builder = new StringBuilder();
        Builder.Append("{");
        Builder.Append("\"r\":" + i + "");
        Builder.Append(",\"data\":\"" + msg + "\"");
        Builder.Append("}");
        return Builder.ToString();
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}